// pages/cardbark/order/order.js
let app = getApp();
let testAddr = app.globalData.testAddr;
let imgurl = app.globalData.imgurl
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arr: [{
      id: "0",
      logname: "全部"
    },
    {
      id: "1",
      logname: "待付款"
    },
    {
      id: "2",
      logname: "待发货"
    },
    {
      id: "3",
      logname: "待收货"
    },
    {
      id: "4",
      logname: "待评价"
    }
    ],
    currentTabsIndex: 0,
    allorderlist:[],
    pageIndex: 1,
    orderID:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that= this
    var id = options.guestID
    that.setData({
      id:id
    })
    that.lookorder()
  },
  onTabsItemTap: function (event) {
    var that = this
    var index = event.currentTarget.dataset.index;
    that.setData({
      currentTabsIndex: index
    });
    that.setData({
      pageIndex: 1,
      allorderlist: [],
    })
    that.lookorder()
  },
  // 查询订单和状态
  lookorder: function (){
    var that= this
    var orderStatus=that.data.currentTabsIndex
    var pageIndex = that.data.pageIndex
    var allorderlist=that.data.allorderlist
    var guestID=that.data.id
     app.util.request({
       url: testAddr + "facility_order/getOrderList?guestID=" + 55 + "&pageIndex=" + pageIndex + "&orderStatus=" + orderStatus +"&pageSize="+5,
       method:"POST",
       data:{},
       success:function(res){
         var orderlist=res.data
         if (orderlist.length!=0){
           that.setData({
             pageIndex: pageIndex+1
           })
         }
         for (var i = 0; i < orderlist.length;i++){
         orderlist[i].offer.offerDetailImg = imgurl +'offer/'+orderlist[i].offer.offerDetailImg
         }
         allorderlist = allorderlist.concat(orderlist)
         that.setData({
           allorderlist: allorderlist
         })
         console.log(allorderlist)
       }
     })
  },
  // 删除订单
  delorder:function(e){
    var that =this
    var orderID = e.currentTarget.dataset.orderid
    that.setData({
      orderID: orderID
    })
    wx.showModal({
      title: '提示',
      content: '确定删除吗？',
      success(res) {
        if (res.confirm) {
          that.del_ture()
        }
        else if (res.cancel) {
        }
      }
    })
  },
  del_ture:function(e){
    var that = this
    var orderID = that.data.orderID
    var allorderlist = that.data.allorderlist
    app.util.request({
      url: testAddr + "facility_order/delOrder?orderID=" + orderID,
      method: "POST",
      data: {},
      success: function (res) {
        that.setData({
          allorderlist: [],
          pageIndex: 1
        })
        that.lookorder()
      }
    })
  },
  // 跳转订单详情
  toorderdetail:function(e){
    var that =this
    var orderID = e.currentTarget.dataset.orderidr
    wx.navigateTo({
      url: '../orderdetail/orderdetail?orderID=' + orderID,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
   var that=this
   that.lookorder()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})